﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Drawing;

public partial class reco_member : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Panel3.Controls.Clear();
        Panel4.Controls.Clear();
        int sid = int.Parse(Session["mid"].ToString());
        DBaccess db = new DBaccess();
        DataSet ds1 = new DataSet();
        DataSet ds2 = new DataSet();
        DataSet ds3 = new DataSet();
        DataSet ds4 = new DataSet();
        DataSet ds5 = new DataSet();
        String sql1 = "select download_weight from mr_member where mid = '" + sid + "'";
        String sql2 = "select preference_weight from mr_member where mid = '" + sid + "'";
        String sql3 = "select time_weight from mr_member where mid = '" + sid + "'";
        String sql4 = "select difficulty_weight from mr_member where mid = '" + sid + "'";
        ds1 = db.GetDataSet(sql1);
        TextBoxDownload.Text = ds1.Tables[0].Rows[0][0].ToString();
        ds2 = db.GetDataSet(sql2);
        TextBoxPreference.Text = ds2.Tables[0].Rows[0][0].ToString();
        ds3 = db.GetDataSet(sql3);
        TextBoxTime.Text = ds3.Tables[0].Rows[0][0].ToString();
        ds4 = db.GetDataSet(sql4);
        TextBoxGPA.Text = ds4.Tables[0].Rows[0][0].ToString();
        sql1 = "select value from mr_parameter where pname = 'sim'";
        sql2 = "select value from mr_parameter where pname = 'GPAperscore'";
        ds1 = db.GetDataSet(sql1);
        TextBoxSim.Text = ds1.Tables[0].Rows[0][0].ToString();
        ds2 = db.GetDataSet(sql2);
        TextGPAperscore.Text = ds2.Tables[0].Rows[0][0].ToString();

        if (LabelMid.Text != "")
        {
            int mid = int.Parse(LabelMid.Text);
            DataSet ds = new DataSet();
            ds = db.GetDataSet("select value from mr_parameter where pname='sim'");
            MyVector mvall = new MyVector(mid);
            MyVector mvreco = new MyVector();
            MyVector mvnoreco = new MyVector();
            mvall.Recommand(double.Parse(ds.Tables[0].Rows[0][0].ToString()));
            mvall.Sort();
            mvreco = mvall.GetRecommand();
            mvnoreco = mvall.GetNoRecommand();
            int i;
            for (i = 0; i < mvnoreco.GetLength(); i++)
            {
                Label lab = new Label();
                lab.Width = 195;
                if (i % 4 == 2 || i % 4 == 3) lab.BackColor = Color.FromArgb(255, 255, 150);
                lab.Text = mvnoreco.GetIname(i) + ":" + mvnoreco.GetValue(i).ToString("0.00");
                Panel3.Controls.Add(lab);
            }
            for (i = 0; i < mvreco.GetLength(); i++)
            {
                LinkButton lbn = new LinkButton();
                lbn.Width = 195;
                if (i % 4 == 2 || i % 4 == 3) lbn.BackColor = Color.FromArgb(255, 255, 150);
                lbn.Text = mvreco.GetIname(i) + ":" + mvreco.GetValue(i).ToString("0.00");
                lbn.CommandArgument = mvreco.GetIname(i);
                lbn.Command += new CommandEventHandler(this.test_Click);
                Panel4.Controls.Add(lbn);
            }
            if (mvnoreco.GetLength() == 0)
            {
                Label lab = new Label();
                lab.Width = 195;
                lab.Text = "无已评分的课程";
                Panel3.Controls.Add(lab);
            }
            if (mvreco.GetLength() == 0)
            {
                Label lab = new Label();
                lab.Width = 195;
                lab.Text = "所有课程都已评分";
                Panel4.Controls.Add(lab);
            }
        }
        DataSet ds7 = new DataSet();
        int way = int.Parse(DropDownList1.SelectedValue);
        String content = TextBoxQuery.Text;
        if (content == "")
        {
        }
        else
        {
            if (way == 1)
                ds7 = db.GetDataSet("select * from mr_member where mid = '" + content + "' and gid = 2");
            else if (way == 2)
                ds7 = db.GetDataSet("select * from mr_member where mname like '%" + content + "%' and gid = 2");
            int i;
            for (i = 0; i < ds7.Tables[0].Rows.Count; i++)
            {
                LinkButton lb = new LinkButton();
                lb.Text = ds7.Tables[0].Rows[i]["mname"].ToString();
                lb.Width = 180;
                lb.CommandArgument = ds7.Tables[0].Rows[i]["mid"].ToString();
                lb.Command += new CommandEventHandler(this.lb_Click);
                Panel1.Controls.Add(lb);
            }
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        String sid = Session["mid"].ToString();
        if (double.Parse(TextBoxSim.Text) < 1)
        {
            DBaccess db = new DBaccess();
            String[] sql = new String[2];
            sql[0] = "update mr_parameter set sim = '" + TextBoxSim.Text + "'";
            sql[1] = "update mr_parameter set GPAperscore = '" + TextGPAperscore.Text + "'";
            db.ExecTranscation(sql);
            Response.Write("<script>alert('修改成功!');</script>");
        }
        else
            Response.Write("<script>alert('用户筛选相似度小于1');</script>");
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ButtonQuery_Click(object sender, EventArgs e)
    {
        Panel3.Controls.Clear();
        Panel4.Controls.Clear();
        int way = int.Parse(DropDownList1.SelectedValue);
        String content = TextBoxQuery.Text;
        if(content == "")
        {
            Response.Write("<script>alert('请输入学号或姓名');</script>");
        }
        else
        {
            if (way == 0)
                Response.Write("<script>alert('请选择查询方式');</script>");
        }
        LabelMid.Text = "";
    }

    public void lb_Click(object sender, CommandEventArgs e)
    {
        Panel3.Controls.Clear();
        Panel4.Controls.Clear();
        int mid = int.Parse(e.CommandArgument.ToString());
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        string sql = "select mname,dname,gpa,score, logo from mr_member a,mr_student b,mr_department c where a.mid = b.sid and b.department=c.did and mid='" + mid.ToString() + "'";
        ds = db.GetDataSet(sql);
        LabelMid.Text = mid.ToString();
        Image1.ImageUrl = ResolveUrl(ds.Tables[0].Rows[0]["logo"].ToString());
        LabelMname.Text = "姓名：" + ds.Tables[0].Rows[0]["mname"].ToString();
        LabelDepart.Text = "学院：" + ds.Tables[0].Rows[0]["dname"].ToString();
        LabelGPA.Text = "绩点：" + ds.Tables[0].Rows[0]["gpa"].ToString();
        LabelScore.Text = "积分" + ds.Tables[0].Rows[0]["score"].ToString();

        ds = db.GetDataSet("select value from mr_parameter where pname='sim'");
        MyVector mvall = new MyVector(mid);
        MyVector mvreco = new MyVector();
        MyVector mvnoreco = new MyVector();
        mvall.Recommand(double.Parse(ds.Tables[0].Rows[0][0].ToString()));
        mvall.Sort();
        mvreco = mvall.GetRecommand();
        mvnoreco = mvall.GetNoRecommand();
        int i;
        for (i = 0; i < mvnoreco.GetLength(); i++)
        {
            Label lab = new Label();
            lab.Width = 195;
            if (i % 4 == 2 || i % 4 == 3) lab.BackColor = Color.FromArgb(255, 255, 150);
            lab.Text = mvnoreco.GetIname(i) + ":" + mvnoreco.GetValue(i).ToString("0.00");
            Panel3.Controls.Add(lab);
        }
        for (i = 0; i < mvreco.GetLength(); i++)
        {
            LinkButton lbn = new LinkButton();
            lbn.Width = 195;
            if (i % 4 == 2 || i % 4 == 3) lbn.BackColor = Color.FromArgb(255, 255, 150);
            lbn.Text = mvreco.GetIname(i) + ":" + mvreco.GetValue(i).ToString("0.00");
            lbn.CommandArgument = mvreco.GetIname(i);
            lbn.Command += new CommandEventHandler(this.test_Click);
            Panel4.Controls.Add(lbn);
        }
        if (mvnoreco.GetLength() == 0)
        {
            Label lab = new Label();
            lab.Width = 195;
            lab.Text = "无已评分的课程";
            Panel3.Controls.Add(lab);
        }
        if (mvreco.GetLength() == 0)
        {
            Label lab = new Label();
            lab.Width = 195;
            lab.Text = "所有课程都已评分";
            Panel4.Controls.Add(lab);
        }
    }

    public void test_Click(object sender, CommandEventArgs e)
    {
        con_step1.SetStep(int.Parse(LabelMid.Text), e.CommandArgument.ToString());
        con_step1.Visible = true;
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        String sid = Session["mid"].ToString();
        if (double.Parse(TextBoxDownload.Text) + double.Parse(TextBoxPreference.Text) + double.Parse(TextBoxTime.Text) + double.Parse(TextBoxGPA.Text) == 1)
        {
            DBaccess db = new DBaccess();
            String[] sql = new String[5];
            sql[0] = "update mr_parameter set value = '" + TextBoxDownload.Text + "' where pname = 'download'";
            sql[1] = "update mr_parameter set value = '" + TextBoxPreference.Text + "' where pname = 'preference'";
            sql[0] = "update mr_parameter set value = '" + TextBoxTime.Text + "' where pname = 'time'";
            sql[0] = "update mr_parameter set value = '" + TextBoxGPA.Text + "' where pname = 'difficulty'";
            sql[4] = "EXEC sp_memberinit";
            db.ExecTranscation(sql);

            Response.Write("<script>alert('推送成功!');</script>");
        }
        else
            Response.Write("<script>alert('请确保各权值总和为100%');</script>");
    }
}